package com.ec.system.modules.sys.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ec.core.common.core.api.ResponseMsg;
import com.ec.core.common.core.api.Result;
import com.ec.core.common.core.base.BasePageVO;
import com.ec.core.common.utils.BeanUtilEx;
import com.ec.core.mybatis.groups.AddGroup;
import com.ec.core.mybatis.groups.UpdateGroup;
import com.ec.core.secure.annotations.PreAuth;
import com.ec.system.modules.sys.dto.QueryDictDTO;
import com.ec.system.modules.sys.dto.SysDictDTO;
import com.ec.system.modules.sys.entity.SysDict;
import com.ec.system.modules.sys.entity.SysDictType;
import com.ec.system.modules.sys.service.ISysDictService;
import com.ec.system.modules.sys.service.ISysDictTypeService;
import com.ec.system.modules.sys.vo.SysDictVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 系统字典明细表 前端控制器
 * </p>
 *
 * @author Eric
 * @since 2022-06-02
 */
@RestController
@RequestMapping("/dict")
public class SysDictController {

    @Autowired
    private ISysDictService dictService;
    @Autowired
    private ISysDictTypeService sysDictTypeService;

    /**
     * 查看字典明细
     *
     * @param dictTypeId 字典id
     * @param dto
     * @return
     */
    @GetMapping("/{dictTypeId}")
    @PreAuth("sys_dict_view")
    public Result<BasePageVO<SysDictVO>> listDict(@PathVariable long dictTypeId, QueryDictDTO dto) {
        return ResponseMsg.success(dictService.listDict(dictTypeId, dto));
    }

    /**
     * 新增字典明细
     *
     * @param dto
     * @return
     */
    @PostMapping
    @PreAuth("sys_dict_add")
    public Result<Void> addDict(@Validated(AddGroup.class) @RequestBody SysDictDTO dto) {
        SysDict dict = new SysDict();
        dto.setDictId(null);
        BeanUtil.copyProperties(dto, dict);
        return ResponseMsg.status(dictService.save(dict));
    }

    /**
     * 修改字典明细
     *
     * @param dto
     * @return
     */
    @PutMapping
    @PreAuth("sys_dict_update")
    public Result<Void> updateDict(@Validated(UpdateGroup.class) @RequestBody SysDictDTO dto) {
        SysDict dict = new SysDict();
        //key和字典id不能修改，避免出现数据错乱
        dto.setDictKey(null);
        dto.setDictTypeId(null);
        BeanUtil.copyProperties(dto, dict);
        return ResponseMsg.status(dictService.updateById(dict));
    }

    /**
     * 删除字典明细
     *
     * @param dictId
     * @return
     */
    @DeleteMapping("/{dictId}")
    @PreAuth("sys_dict_delete")
    public Result<Void> deleteDict(@PathVariable long dictId) {
        return ResponseMsg.status(dictService.removeById(dictId));
    }

    /**
     * 根据字典编码查询所有字典明细
     *
     * @param dictTypeCode
     * @return
     */
    @PreAuth("sys_dict_view")
    @GetMapping("/queryDictList/{dictTypeCode}")
    Result<List<SysDictVO>> querySysDictByDictTypeCode(@PathVariable("dictTypeCode") String dictTypeCode) {
        SysDictType sysDictType = sysDictTypeService.getOne(new LambdaQueryWrapper<SysDictType>().eq(SysDictType::getDictTypeCode, dictTypeCode));
        if (sysDictType != null) {
            List<SysDict> sysDictList = dictService.list(new LambdaQueryWrapper<SysDict>().eq(SysDict::getDictTypeId, sysDictType.getDictTypeId()));
            if (!CollectionUtils.isEmpty(sysDictList)) {
                List<SysDictVO> sysDictVOList = BeanUtilEx.copyList(sysDictList, SysDictVO.class);
                return ResponseMsg.success(sysDictVOList);
            }
        }
        return ResponseMsg.error();
    }
}
